package com.cgx.tablet.beeper.glide;

import android.app.ActivityManager;
import android.content.Context;
import android.os.Build;

import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.annotation.GlideModule;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool;
import com.bumptech.glide.load.engine.cache.ExternalCacheDiskCacheFactory;
import com.bumptech.glide.load.engine.cache.LruResourceCache;
import com.bumptech.glide.load.engine.cache.MemorySizeCalculator;
import com.bumptech.glide.module.AppGlideModule;

/**
 * 定制Glide各种策略
 * Created by admin on 2016/11/4.
 */
@GlideModule
public class GlideConfiguration extends AppGlideModule {

    @Override
    public void applyOptions(Context context, GlideBuilder builder) {
        // Prefer higher quality images unless we're on a low RAM device
        MemorySizeCalculator calculator = new MemorySizeCalculator.Builder(context)
                .setMemoryCacheScreens(2)
                .build();
        int defaultMemoryCacheSize = calculator.getMemoryCacheSize();
        int defaultBitmapPoolSize = calculator.getBitmapPoolSize();

        int customMemoryCacheSize = (int) (1.2 * defaultMemoryCacheSize);
        int customBitmapPoolSize = (int) (1.2 * defaultBitmapPoolSize);

        //1、内存缓存相关
        builder.setMemoryCache(new LruResourceCache(customMemoryCacheSize));
        builder.setBitmapPool(new LruBitmapPool(customBitmapPoolSize));

        // set size & external vs. internal
        int cacheSize100MegaBytes = 104857600;
        //2、磁盘缓存相关
        builder.setDiskCache(
//                new InternalCacheDiskCacheFactory(context, cacheSize100MegaBytes)//内部使用的磁盘缓存区
                new ExternalCacheDiskCacheFactory(context, cacheSize100MegaBytes)//外部可以访问的磁盘缓存区

        );


        ActivityManager activityManager =
                (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
//        //3、根据运行内存情况，自定义对图像的编码格式
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            builder.setDecodeFormat(activityManager.isLowRamDevice() ?
//                    DecodeFormat.PREFER_RGB_565 : DecodeFormat.PREFER_ARGB_8888);
//        } else {
//            builder.setDecodeFormat(DecodeFormat.PREFER_RGB_565);
//        }
    }

    @Override
    public boolean isManifestParsingEnabled() {
        return false;
    }
}
